<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view class="address-btn u-flex u-row-center" v-if="!address">
        <u-button
          shape="circle"
          type="error"
          plain
          size="medium"
          @click="handleChooseAddress"
          :custom-style="{ backgroundColor: '#fff!important' }"
          >获取收货地址</u-button
        >
      </view>

      <view v-else class="address-detail">
        <view class="address-detail">{{ addressDetail }}</view>
        <view class="address-user"
          >{{ address.userName }} {{ address.telNumber }}
        </view>
      </view>
    </view>
    <u-line color="#eee"></u-line>
    <!-- 2 已选商品 -->
    <view class="goods-list u-p-15">
      <view class="goods-list-title">已选商品</view>
      <view class="goods-list-content">
        <view
          class="cart-item"
          v-for="item in checkedCartList"
          :key="item.goods_id"
        >
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image
              width="191rpx"
              height="191rpx"
              :src="item.goods_small_logo"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{ item.goods_name }}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{ item.goods_price }}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">X{{ item.nums }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price">
        合计: <text>￥{{ checkedPrice }}</text>
      </view>
      <view class="all-count">
        <u-button @click="handlePay" type="error" shape="circle"
          >去支付({{ checkedCount }})</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      address: null,
    };
  },
  computed: {
    ...mapGetters("cart", ["checkedCartList", "checkedPrice", "checkedCount"]),
    // 详细地址
    addressDetail() {
      if (!this.address) return "";
      return (
        this.address.provinceName +
        this.address.cityName +
        this.address.countyName +
        this.address.detailInfo
      );
    },
  },
  methods: {
    async handleChooseAddress() {
      const [err, res] = await uni.chooseAddress();
      if (!err) {
        this.address = res;
      }
    },
    // 点击结算按钮
    async handlePay() {
      // 0 判断有没有收货地址
      if (!this.address) {
        uni.showToast({
          title: "您还没有选择收货地址",
          icon: "none",
        });
        return;
      }
      // 1 获取用户信息
      const [err, { encryptedData, rawData, iv, signature }] =
        await uni.getUserProfile({
          desc: "用于提供更好的服务",
        });
      // console.log(encryptedData, rawData, iv, signature);

      // 2 获取 code 临时凭据
      const [err2, { code }] = await uni.login();
    //   console.log(code);

      // 3 获取token  后端没有写好代码！！ 正常是每一次获取都是ok！！！
      // 以点击两次 为准
      const { token } = await this.$u.post("/users/wxlogin", {
        encryptedData,
        rawData,
        iv,
        signature,
        code,
      });
    //   console.log(token);
     // 4 准备创建订单要的参数
      const orderParams = {
        order_price: this.checkedPrice,
        // A | B | C
        consignee_addr: `${this.addressDetail}|${this.address.userName}|${this.address.telNumber}`,
        goods: this.checkedCartList.map((item) => ({
          goods_id: item.goods_id,
          goods_number: item.nums,
          goods_price: item.goods_price,
        })),
      };

      // 5 发送请求 获取 订单编号 axios 传参的写法 我不确定！！
      const { order_number } = await this.$u.post(
        "/my/orders/create",
        orderParams,
        {
          Authorization: token,
        }
      );
        // 6 创建支付流程 获取支付参数
      const { pay } = await this.$u.post(
        "/my/orders/req_unifiedorder",
        { order_number },
        { Authorization: token }
      );
        // 7 调起微信支付
      const res7 = await uni.requestPayment(pay);
      console.log(res7);
    },
  },
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 111rpx;
}
.cart-item {
  display: flex;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;

  .goods-img {
    flex: 2;

    display: flex;
    align-items: center;
    justify-content: center;
  }

  .goods-info {
    color: #666;
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .goods-name {
    }

    .goods-price-num {
      display: flex;
      justify-content: space-between;
      .goods-price {
        color: $themeColor;
      }

      .goods-num-tool {
        width: 140rpx;
        height: 40rpx;

        display: flex;
        justify-content: flex-end;

        .goods-vnum {
        }
      }
    }
  }
}
.statistics {
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  border-top: 1rpx solid #ccc;
  display: flex;
  align-items: center;
  padding: 15rpx;
  .all-price {
    flex: 1;
    color: #666;
    text {
    }
  }

  .all-count {
  }
}
</style>

